package com.sy.musicapp.model

import android.accounts.NetworkErrorException
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sy.musicapp.Util
import com.sy.musicapp.intent.api.UserApi
import com.sy.musicapp.intent.network.MusicAppNetwork
import com.sy.musicapp.showToast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

/**
 * 用户模块ViewModel
 * @author SY
 * @since 2022-02-21 21:32
 **/
class UserInfoViewModel : ViewModel(), UserApi {

    var userInfo = MutableStateFlow(UserInfo.Empty)
    var banners = MutableStateFlow(Banner.Empty)
    var recomMusics = MutableStateFlow(listOf<Music>())

    init {
        getBanner()
        getRecomMusic()
    }


    override fun login(username: String, pwd: String, choice: LoginWay, callback: LoginCallback) {
        if (userInfo.value != UserInfo.Empty) {
            callback.sucess()
            return
        }
        viewModelScope.launch(Dispatchers.Main) {
            try {
                when (choice) {
                    LoginWay.Phone -> {
                        MusicAppNetwork.loginWithPhone(username, pwd).body()?.let {
                            if (it.code != 200) throw NetworkErrorException(it.toString())
                            Util.showLog(it)
                            userInfo.value = it
                        }
                    }
//                    未启用邮箱登录
                    LoginWay.Email -> {
                        MusicAppNetwork.loginWithEmail(username, pwd).body()?.let {
                            if (it.code != 200) throw NetworkErrorException(it.toString())
//                            userInfo.value = it
                        }
                    }
                }
                callback.sucess()
            } catch (e: Exception) {
                Util.showLog("error")
                callback.failed()
                e.printStackTrace()
            }
        }
    }


    /** 更新登录状态或检查是否登录 */
    override fun updateStatus(status: (Boolean) -> Unit) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                MusicAppNetwork.updateStatus().body()?.let {
                    Util.showLog(it)
                    status(it.code == 200)
                }
            } catch (e: Exception) {
                "似乎出了点问题，请重新启动".showToast()
                e.printStackTrace()
            }
        }
    }


    override fun getUserInfo() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                MusicAppNetwork.getUserInfo().body()?.let {
                    userInfo.value = it
                }
            } catch (e: Exception) {
                "更新用户信息失败".showToast()
                e.printStackTrace()
            }
        }
    }


    override fun logout(status: (Boolean) -> Unit) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                MusicAppNetwork.logout().body()?.let {
                    if (it.code == 200) {
                        status(true)
                    } else {
                        status(false)
                    }
                }
            } catch (e: Exception) {
                "退出失败".showToast()
                e.printStackTrace()
            }
        }
    }

    override fun getBanner() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                MusicAppNetwork.getBanner().body()?.let {
                    banners.value = it
                }
            } catch (e: Exception) {
                "获取海报失败".showToast()
                e.printStackTrace()
            }
        }
    }

    override fun getRecomMusic() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                MusicAppNetwork.getRecomMusic().body()?.let {
                    recomMusics.value = it
                }
            } catch (e: Exception) {
                "获取推荐失败".showToast()
                e.printStackTrace()
            }
        }
    }

    fun resetAll() {
        userInfo = MutableStateFlow(UserInfo.Empty)
    }


    interface LoginCallback {
        fun sucess()
        fun failed()
    }


}